
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_getx/res/colors/colors.dart';
import 'package:flutter_getx/res/dimens/dimens.dart';
import 'package:flutter_getx/res/gaps.dart';
import 'package:flutter_getx/res/styles.dart';

import 'my_button.dart';


/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {

  const MyAppBar({
    Key? key,
    this.backgroundColor,
    this.title = '',
    this.centerTitle = '',
    this.actionName = '',
    this.backImg = 'assets/images/ic_arrow_back.png',
    this.backImgColor,
    this.onPressed,
    this.onPressedActions1,
    this.isBack = true,
    this.linearGradient,
    this.actions1Widget ,
    this.diyactions,
    this.onBack,
  }): super(key: key);

  final Color? backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final Color? backImgColor;
  final String actionName;
  final List<Widget>? actions1Widget;
  final VoidCallback? onPressed;
  final VoidCallback? onPressedActions1;
  final bool isBack;
  final List<Color>? linearGradient;
  final Widget? diyactions;
  final VoidCallback? onBack;

  @override
  Widget build(BuildContext context) {
    final Color _backgroundColor = backgroundColor ?? colorBg;

    final SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(_backgroundColor) == Brightness.dark
        ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;

    final Widget back = isBack ? IconButton(
      onPressed: onBack ?? () async {
        FocusManager.instance.primaryFocus?.unfocus();
        final isBack = await Navigator.maybePop(context);
        if (!isBack) {
          await SystemNavigator.pop();
        }
      },
      tooltip: 'Back',
      padding: const EdgeInsets.all(12.0),
      icon: Image.asset(
        backImg,
        color: backImgColor ?? colorIcon,
      ),
    ) : Gaps.empty;

    final Widget action = actionName.isNotEmpty ? Positioned(
      right: 0.0,
      child: Theme(
        data: Theme.of(context).copyWith(
          buttonTheme: const ButtonThemeData(
            padding: EdgeInsets.symmetric(horizontal: 16.0),
            minWidth: 60.0,
          ),
        ),
        child: MyButton(
          key: const Key('actionName'),
          fontSize: Sp.sp14,
          minWidth: null,
          text: actionName,
          textColor: colorText,
          backgroundColor: Colors.transparent,
          onPressed: onPressed,
        ),
      ),
    ) : Gaps.empty;

    final Widget action1 = actions1Widget != null ? Positioned(
      right: 0.0,
      child:Row(
        children: actions1Widget??[],
      )
    ): Gaps.empty;

    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        child: Text(
          title.isEmpty ? centerTitle : title,
          style: appbarBlack,
        ),
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
      ),
    );
    
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: _backgroundColor,
        child: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: linearGradient??[Colors.transparent,Colors.transparent],
              )),
          child: SafeArea(
            child: Stack(
              alignment: Alignment.centerLeft,
              children: <Widget>[
                titleWidget,
                back,
                action,
                action1,
                diyactions??Gaps.empty
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(55.0);
}
